home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / collate.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  9.3 KB  |  297 lines

  1. #ifndef __STD_COLLATE__
  2. #define __STD_COLLATE__
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * collate - Declarations for the Standard Library character
  8.  * collation facet
  9.  *
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  14.  * ALL RIGHTS RESERVED *
  15.  * The software and information contained herein are proprietary to, and
  16.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  17.  * intends to preserve as trade secrets such software and information.
  18.  * This software is furnished pursuant to a written license agreement and
  19.  * may be used, copied, transmitted, and stored only in accordance with
  20.  * the terms of such license and with the inclusion of the above copyright
  21.  * notice.  This software and information or any other copies thereof may
  22.  * not be provided or otherwise made available to any other person.
  23.  *
  24.  * Notwithstanding any other lease or license that may pertain to, or
  25.  * accompany the delivery of, this computer software and information, the
  26.  * rights of the Government regarding its use, reproduction and disclosure
  27.  * are as set forth in Section 52.227-19 of the FARS Computer
  28.  * Software-Restricted Rights clause.
  29.  *
  30.  * Use, duplication, or disclosure by the Government is subject to
  31.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  32.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  33.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  34.  * P.O. Box 2328, Corvallis, Oregon 97339.
  35.  *
  36.  * This computer software and information is distributed with "restricted
  37.  * rights."  Use, duplication or disclosure is subject to restrictions as
  38.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  39.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  40.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  41.  * then the "Alternate III" clause applies.
  42.  *
  43.  **************************************************************************/
  44.  
  45.  
  46. #ifndef __STD_RWLOCALE__
  47. #include "rw/rwlocale.h"
  48. #endif
  49.  
  50. #ifndef _RWSTD_NO_NAMESPACE
  51. namespace __rwstd {
  52. #endif
  53.  
  54. // ------------------------------------------------------
  55. // Implementation class template -- collate_table<charT>.
  56. // ------------------------------------------------------
  57.  
  58. // Data structure that drives the collate<charT> process.  It contains a
  59. // binary tree of nodes, each of which defines the collation treatment to be
  60. // applied to a range of characters.
  61.  
  62. template <class charT>
  63. class _RWSTDExportTemplate collate_table {
  64.  public:
  65.   struct node {
  66.     node *left;             // Node for charT values that are too small (resp.
  67.     node *right;            //  too large) for this node
  68.     const charT *table;     // Translate table or NULL
  69.     charT minC,maxC;        // Range of charT-s covered by this node
  70.     charT offset;           // Amount to add to charT if table is NULL
  71.   };
  72.  
  73.   long length;              // Length of memory block containing all the nodes
  74.   node root;                // Root node of table
  75. };
  76.  
  77.  
  78. // -----------------------------------------------------
  79. // Implementation class template -- collate_data<charT>.
  80. // -----------------------------------------------------
  81.  
  82. // collate<charT> derives from this (via rwstd::collate_impl) to get its
  83. // private data members.
  84.  
  85. template <class charT>
  86. class _RWSTDExportTemplate collate_data
  87. {
  88.  public:
  89.   typedef collate_table<charT> table;
  90.   typedef _TYPENAME table::node node;
  91.  
  92.   const table *table_;
  93.  
  94.   collate_data (const table &t): table_(&t) { }
  95.   charT coll_order (charT) const;
  96. };
  97.  
  98. template <class charT>
  99. _RWSTD_TRICKY_INLINE charT _RWSTDExportTemplate collate_data<charT>::
  100.     coll_order (charT c) const
  101. {
  102.   const node *t=&table_->root;
  103.   while (t)
  104.     if (c < t->minC)
  105.       t=t->left;
  106.     else if (c > t->maxC)
  107.       t=t->right;
  108.     else {
  109.       if (t->table)
  110.         c=t->table[size_t(c-t->minC)];
  111.       else
  112.         c+=t->offset;
  113.       break;
  114.     }
  115.  
  116.   return c;
  117. }
  118.  
  119. // -----------------------------------------------------
  120. // Implementation class template -- collate_impl<charT>.
  121. // -----------------------------------------------------
  122.  
  123. // Facet collate<charT> derives from this to get the part of its behavior that
  124. // is specialized for char and wchar_t.
  125.  
  126. template <class charT>
  127. class _RWSTDExportTemplate collate_impl :
  128.     public collate_data<charT>
  129. {
  130.   static collate_table<charT> _RWSTDExport default_table_;
  131.  public:
  132.   collate_impl (void):
  133.       collate_data<charT>(default_table_) { }
  134. };
  135.  
  136. // Specialization for char.
  137. _RWSTD_TEMPLATE
  138. class _RWSTDExport collate_impl<char>:
  139.     public collate_data<char>
  140. {
  141.   static collate_table<char> default_table_;
  142.  public:
  143.   collate_impl (void):
  144.       collate_data<char>(default_table_) { }
  145. };
  146.  
  147.  
  148. // Specialization for wchar_t.
  149. #ifndef _RWSTD_NO_WIDE_CHAR
  150. _RWSTD_TEMPLATE
  151. class _RWSTDExport collate_impl<wchar_t>:
  152.     public collate_data<wchar_t>
  153. {
  154.   static collate_table<wchar_t> default_table_;
  155.  public:
  156.   collate_impl (void):
  157.       collate_data<wchar_t>(default_table_) { }
  158. };
  159. #endif
  160.  
  161.  
  162. #ifndef _RWSTD_NO_NAMESPACE
  163. } namespace std {
  164. #endif
  165.  
  166. // -----------------------------------------------------
  167. // Standard character collation facet -- collate<charT>.
  168. // -----------------------------------------------------
  169.  
  170. template <class charT>
  171. class  collate: public locale::facet,
  172.     public __RWSTD::collate_impl<charT>
  173. {
  174.  public:
  175.   typedef charT char_type;
  176.   typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;
  177.  
  178.   _EXPLICIT collate (size_t refs=0):
  179.       locale::facet(refs,locale::_rw_collate_category)
  180.     { }
  181.  
  182.   int compare (const charT* low1, const charT* high1,
  183.                const charT* low2, const charT* high2) const
  184.     { return do_compare(low1, high1, low2, high2); }
  185.  
  186.   string_type transform (const charT* low, const charT* high) const
  187.     { return do_transform(low, high); }
  188.  
  189.   long hash (const charT* low, const charT* high) const
  190.     { return do_hash(low, high); }
  191.  
  192.   static locale::id id;
  193.  
  194.   // Implementation:
  195.   enum { facet_cat_ = locale::_rw_collate_category, ok_implicit_ = 1 };
  196.  
  197.  protected:
  198.   virtual ~collate();
  199.  
  200.   virtual int do_compare (const charT* low1, const charT* high1,
  201.                           const charT* low2, const charT* high2) const;
  202.   virtual string_type do_transform (const charT* low, const charT* high) const;
  203.   virtual long do_hash (const charT* low, const charT* high) const;
  204.  
  205.  private:
  206.   #ifdef _RWSTD_NO_MEMBER_TEMPLATES
  207.   locale::id &get_id (void) const { return id; }
  208.   #endif
  209. };
  210.  
  211. // ------------------------------------------------------
  212. // Standard facet specialization -- collate_byname<char>.
  213. // ------------------------------------------------------
  214. template <class charT>
  215. class _RWSTDExport collate_byname;
  216.  
  217. _RWSTD_TEMPLATE
  218. class _RWSTDExport collate_byname<char>: public collate<char> {
  219.  public:
  220.   _EXPLICIT collate_byname (const char*, size_t refs=0);
  221.  protected:
  222.   virtual ~collate_byname ();
  223.  
  224.   // Virtual member functions, override functions in collate<char>.
  225.   virtual int do_compare (const char* low1, const char* high1,
  226.                           const char* low2, const char* high2) const;
  227.   virtual string do_transform (const char* low, const char* high) const;
  228.  
  229.   // The remaining virtual function (do_hash) is inherited from collate<char>
  230.   // in this implementation.
  231.  
  232.  private:
  233.   // Implementation.
  234.   string collate_name_;
  235. };
  236.  
  237. // ------------------------------------------------
  238. // Standard derived facet -- collate_byname<charT>.
  239. // ------------------------------------------------
  240.  
  241. template <class charT>
  242. class _RWSTDExport collate_byname: public collate<charT> {
  243.  public:
  244.   typedef basic_string<charT,char_traits<charT>,
  245.                                 allocator<charT> > string_type;
  246.  
  247.   _EXPLICIT collate_byname (const char*, size_t refs=0);
  248.  
  249.  protected:
  250.   virtual ~collate_byname();
  251.  
  252.   // Virtual member functions overridden from collate<charT>.
  253.   virtual int do_compare (const charT* low1, const charT* high1,
  254.                           const charT* low2, const charT* high2) const;
  255.   virtual string_type do_transform (const charT* low, const charT* high) const;
  256.  
  257.   // The remaining virtual function (do_hash) is inherited from collate<charT>
  258.   // in this implementation.
  259.  
  260.  private:
  261.   // Implementation.
  262.   string_type collate_name_;
  263. };
  264.  
  265. #ifndef _RWSTD_NO_NAMESPACE
  266. } namespace __rwstd {
  267. #endif
  268.  
  269. #ifndef _RWSTD_NO_FUNC_PARTIAL_SPEC
  270. template <class charT>
  271. inline collate<charT>* _RWSTDExport create_named_facet
  272.     (collate<charT>*,const char *name,size_t refs)
  273. { return new collate_byname<charT>(name,refs); }
  274. #else
  275. _RWSTD_TEMPLATE
  276. inline _STD::collate<char>* _RWSTDExport create_named_facet
  277.     (_STD::collate<char>*,const char *name,size_t refs)
  278. { return new collate_byname<char>(name,refs); }
  279. #ifndef _RWSTD_NO_WIDE_CHAR
  280. _RWSTD_TEMPLATE
  281. inline _STD::collate<wchar_t>* _RWSTDExport create_named_facet
  282.     (_STD::collate<wchar_t>*,const char *name,size_t refs)
  283. { return new collate_byname<wchar_t>(name,refs); }
  284. #endif
  285. #endif
  286.  
  287. #ifndef _RWSTD_NO_NAMESPACE
  288. #endif
  289.  
  290. #ifdef _RWSTD_COMPILE_INSTANTIATE
  291. #include <rw/collate.cc>
  292. #endif
  293.  
  294. #pragma option pop
  295. #endif // __STD_COLLATE__
  296.